//
// File:	GuiElements.cpp
// Author:	Thomas TISSOT-DUPONT
// E-mail:	<thomas.tissotdupont@free.fr>
// Create:	07/01/2010 21:48:53
// Purpose:	
//

#include <QtGui/QFont>
#include "GuiElements.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Utils
//------------------------------------------------------------------
void GuiElementUtils::UpdateWidgetFont(QWidget &widget, float flRatioWidth, float flRatioHeight)
{
	QFont newFont = widget.font();
	newFont.setPointSizeF(newFont.pointSizeF() * flRatioHeight);
	widget.setFont(newFont);
}

void GuiElementUtils::UpdateWidgetSize(QWidget &widget, float flRatioWidth, float flRatioHeight)
{
	widget.resize(widget.width() * flRatioWidth, widget.height() * flRatioHeight);
}

void GuiElementUtils::UpdateWidgetPosition(QWidget &widget, float flRatioWidth, float flRatioHeight)
{
	widget.move(widget.x() * flRatioWidth, widget.y() * flRatioHeight);
}

//------------------------------------------------------------------
// GuiLabel
//------------------------------------------------------------------
GuiLabel::GuiLabel(QWidget *pParent /* = NULL */)
	:	QLabel(pParent)
{
}

void GuiLabel::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
}

//------------------------------------------------------------------
// GuiImage
//------------------------------------------------------------------
GuiImage::GuiImage(QWidget *pParent /* = NULL */)
	:	QLabel(pParent)
{
}

void GuiImage::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);

	const QPixmap *pPixmap = pixmap();
	if (pPixmap != NULL)
	{
		QPixmap newPixmap = pPixmap->scaled(pPixmap->width() * flRatioWidth,
											pPixmap->height() * flRatioHeight,
											Qt::IgnoreAspectRatio,
											Qt::SmoothTransformation);
		setPixmap(newPixmap);
	}
}

//------------------------------------------------------------------
// GuiGroupBox
//------------------------------------------------------------------
GuiGroupBox::GuiGroupBox(QWidget *pParent /* = NULL */)
	:	QGroupBox(pParent)
{
}

void GuiGroupBox::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);

	//Children
	QObjectList myChildrenList = children();
	QObjectList::iterator it = myChildrenList.begin();
	QObjectList::iterator ite = myChildrenList.end();
	for (; it != ite; ++it)
	{
		IGuiElement *pGuiElement = dynamic_cast<IGuiElement *>(*it);
		if (pGuiElement != NULL)
			pGuiElement->repositionOnScreen(flRatioWidth, flRatioHeight);
	}
}

//------------------------------------------------------------------
// GuiPushButton
//------------------------------------------------------------------
GuiPushButton::GuiPushButton(QWidget *pParent /* = NULL */)
	:	QPushButton(pParent)
{
	QObject::connect(this, SIGNAL(pressed()), this, SLOT(onPressed()));
}

void GuiPushButton::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
}

void GuiPushButton::enterEvent(QEvent *e)
{
	QPushButton::enterEvent(e);
}

void GuiPushButton::onPressed()
{
}

//------------------------------------------------------------------
// GuiComboBox
//------------------------------------------------------------------

GuiComboBox::GuiComboBox(QWidget *pParent /* = NULL */)
	:	QComboBox(pParent)
{
}

void GuiComboBox::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
	QAbstractItemView *pView = view();
	if (pView != NULL)
	{
		GuiElementUtils::UpdateWidgetFont(*pView, flRatioWidth, flRatioHeight);
		GuiElementUtils::UpdateWidgetSize(*pView, flRatioWidth, flRatioHeight);
	}
}

void GuiComboBox::enterEvent(QEvent *e)
{
	QComboBox::enterEvent(e);
}

//------------------------------------------------------------------
// GuiListWidget
//------------------------------------------------------------------
GuiListWidget::GuiListWidget(QWidget *pParent /* = NULL */)
	:	QListWidget(pParent)
{
}

void GuiListWidget::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
}

//------------------------------------------------------------------
// GuiLineEdit
//------------------------------------------------------------------
GuiLineEdit::GuiLineEdit(QWidget *pParent /* = NULL */)
	:	QLineEdit(pParent)
{
}

void GuiLineEdit::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
}

void GuiLineEdit::enterEvent(QEvent *e)
{
	QLineEdit::enterEvent(e);
}

//------------------------------------------------------------------
// GuiTableWidget
//------------------------------------------------------------------
GuiTableWidget::GuiTableWidget(QWidget *pParent /* = NULL */)
	:	QTableWidget(pParent)
{
}

void GuiTableWidget::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);

	//Columns
	for (int i = 0; i < columnCount(); ++i)
	{
		setColumnWidth(i, static_cast<float>(columnWidth(i)) * flRatioWidth);
	}
}

//------------------------------------------------------------------
// GuiCheckBox
//------------------------------------------------------------------
GuiCheckBox::GuiCheckBox(QWidget *pParent /* = NULL */)
	:	QCheckBox(pParent)
{
}

void GuiCheckBox::repositionOnScreen(float flRatioWidth, float flRatioHeight)
{
	GuiElementUtils::UpdateWidgetFont(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetSize(*this, flRatioWidth, flRatioHeight);
	GuiElementUtils::UpdateWidgetPosition(*this, flRatioWidth, flRatioHeight);
}

void GuiCheckBox::enterEvent(QEvent *e)
{
	QCheckBox::enterEvent(e);
}
